package com.cicadamob.contact.api.service.provider;
import com.cicadamob.contact.api.service.ContactDbHelper;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class ContactProvider extends ContentProvider{
	public static final String AUTHORITY = "com.cicadamob.contact.api.contact";  
	   private static final UriMatcher sMatcher;
	   private static final int CompanyContact_ITEM = 1;
	    private static final int CompanyContact_ITEM_ID = 2;
	    private static final int ClientContact_ITEM = 3;
	    private static final int ClientContact_ITEM_ID = 4;
	    private static final int ClientTree_ITEM = 5;
	    private static final int ClientTree_ITEM_ID = 6;
	    private static final int CompanyTree_ITEM = 7;
	    private static final int CompanyTree_ITEM_ID = 8;
	    static{
	            sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	            sMatcher.addURI(AUTHORITY,CompanyContactURI.CompanyContact, CompanyContact_ITEM);
	            sMatcher.addURI(AUTHORITY, CompanyContactURI.CompanyContact+"/#", CompanyContact_ITEM_ID);
	            sMatcher.addURI(AUTHORITY,ClientContactURI.ClientContact, ClientContact_ITEM);
	            sMatcher.addURI(AUTHORITY, ClientContactURI.ClientContact+"/#", ClientContact_ITEM_ID);
	            
	            sMatcher.addURI(AUTHORITY,CompanyTreeURI.COMPANY_TREE, CompanyTree_ITEM);
	            sMatcher.addURI(AUTHORITY, CompanyTreeURI.COMPANY_TREE+"/#", CompanyTree_ITEM_ID);
	            
	            sMatcher.addURI(AUTHORITY, ClientTreeURI.COMPANY_CLIENT_TREE, ClientTree_ITEM);
	            sMatcher.addURI(AUTHORITY, ClientTreeURI.COMPANY_CLIENT_TREE+"/#", ClientTree_ITEM_ID);
	            

	    }
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

    @Override
    public String getType(Uri uri) {
            // TODO Auto-generated method stub
            switch (sMatcher.match(uri)) {
            case CompanyContact_ITEM:
                  return CompanyContactURI.CONTENT_TYPE;
                  
            case CompanyContact_ITEM_ID:
                 return	CompanyContactURI.CONTENT_ITEM_TYPE;
            
            case ClientContact_ITEM:
            	
            	return ClientContactURI.CONTENT_TYPE;
            case ClientContact_ITEM_ID:
            	
            	return ClientContactURI.CONTENT_ITEM_TYPE;
            	
           case CompanyTree_ITEM:
            	
            	return CompanyTreeURI.CONTENT_TYPE;
            case CompanyTree_ITEM_ID:
            	
            	return CompanyTreeURI.CONTENT_ITEM_TYPE;
            	
            case ClientTree_ITEM:
            	
            	return ClientTreeURI.CONTENT_TYPE;
            case ClientTree_ITEM_ID:
            	
            	return ClientTreeURI.CONTENT_ITEM_TYPE;	
            	
            default:
                    throw new IllegalArgumentException("Unknown URI"+uri);
            }
    }

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		SQLiteDatabase database = ContactDbHelper.getInstance().openDatabase();
		if(database == null)
			return null;
		Cursor cursor = null;
		try {
			switch (sMatcher.match(uri)) {
			case CompanyContact_ITEM:
				cursor = database.query(ContactDbHelper.COMPANY_PERSON_DATABASE, projection, selection, selectionArgs, null, null, sortOrder);  
				break;
			case CompanyContact_ITEM_ID:

				break;
			case ClientContact_ITEM:
				cursor = database.query(ContactDbHelper.COMPANY_CLIENT_PERSON_TABLE, projection, selection, selectionArgs, null, null, sortOrder);  

				break;
			case ClientContact_ITEM_ID:
				
				break;
				
			case CompanyTree_ITEM: //同事组织表
				cursor=database.query(ContactDbHelper.COMPANY_TREE_DATABASE, projection, selection, selectionArgs, null, null, sortOrder);
				break;
			case CompanyTree_ITEM_ID:
				
				break;
				
	       case ClientTree_ITEM: //客户组织表
	    	   cursor=database.query(ContactDbHelper.COMPANY_CLIENT_TREE_TABLE, projection, selection, selectionArgs, null, null, sortOrder);
				break;
			case ClientTree_ITEM_ID:
				
				break;
			default:
				throw new IllegalArgumentException("Unknown URI" + uri);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
//			ContactDbHelper.getInstance().closeDatabase();
		}
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
